﻿using System;
using System.Collections.Generic;
using Communication.Message.Buffer;

namespace Communication.Message.Osc
{
    /// <summary>
    /// 控件接受报文处理
    /// </summary>
    public class ControlChannelMessage : OscChannelMessage
    {

        public event DataReceivedEventHandler DataReceived;

        public ControlChannelMessage(int channelCount) : base(2)
        {
            if (channelCount < 1)
            {
                throw new ArgumentOutOfRangeException("channel count must >0");
            }
            this.channelCount = channelCount;
            this.DATA_FRAME_LENGTH = 3 + 2 * channelCount;
            this.sendBuffer = MessageBuffers.Buffer(3);
            this.sendBuffer.WriteByte(byte.MaxValue);
            this.sendBuffer.WriteByte(this.fun);
            byte b = OscUtils.Crc8(this.sendBuffer.Array(), this.sendBuffer.ReaderIndex(), this.sendBuffer.ReadableBytes());
            this.sendBuffer.WriteByte(b);
            this.receiveBuffer = MessageBuffers.DynamicBuffer(4096);
            this.cancelSendBuffer = MessageBuffers.Buffer(3);
            this.cancelSendBuffer.WriteByte(byte.MaxValue);
            this.cancelSendBuffer.WriteByte(3);
            b = OscUtils.Crc8(this.cancelSendBuffer.Array(), this.cancelSendBuffer.ReaderIndex(), this.cancelSendBuffer.ReadableBytes());
            this.cancelSendBuffer.WriteByte(b);
        }
        /// <summary>
        /// 处理接收的报文
        /// </summary>
        /// <returns></returns>
        protected override bool DealwithReceiveMsg()
        {
            if (this.receiveBuffer.ReadableBytes() < 3)
            {
                return false;
            }
            List<short[]> list = new List<short[]>(64);
            bool flag;
            do
            {
                flag = false;
                while (this.receiveBuffer.ReadableBytes() > 0)
                {
                    this.receiveBuffer.MarkReaderIndex();
                    byte b = this.receiveBuffer.ReadByte();
                    if (b == 255)
                    {
                        this.receiveBuffer.ResetReaderIndex();
                        if (this.receiveBuffer.ReadableBytes() < this.DATA_FRAME_LENGTH)
                        {
                            break;
                        }
                        byte @byte = this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + this.DATA_FRAME_LENGTH - 1);
                        byte b2 = OscUtils.Crc8(this.receiveBuffer.Array(), this.receiveBuffer.ReaderIndex(), this.DATA_FRAME_LENGTH - 1);
                        if (@byte != b2)
                        {
                            this.receiveBuffer.ReadByte();
                        }
                        else
                        {
                            short[] array = new short[1 + this.channelCount];
                            this.receiveBuffer.ReadByte();
                            array[0] = (short)this.receiveBuffer.ReadByte();
                            for (int i = 0; i < this.channelCount; i++)
                            {
                                array[1 + i] = this.receiveBuffer.ReadShort(); //大端模式(即高位在前)
                            }
                            this.receiveBuffer.ReadByte();
                            list.Add(array);//获取多个通道的数据
                            flag = true;
                        }
                    }
                }
            }
            while (flag);
            this.receiveBuffer.DiscardReadBytes();
            if (list.Count > 0)
            {
                if (this.DataReceived != null)
                {
                    try
                    {
                        this.DataReceived(list);
                    }
                    catch
                    {
                    }
                }
                return true;
            }
            return false;
        }

        // Token: 0x060001A6 RID: 422 RVA: 0x00005790 File Offset: 0x00003990
        public override int GetCancelSendData(byte[] buffer, int offset)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException();
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new IndexOutOfRangeException();
            }
            return this.cancelSendBuffer.ReadBytes(buffer, offset);
        }

        // Token: 0x0400006B RID: 107
        private MessageBuffer cancelSendBuffer;

        // Token: 0x0400006C RID: 108
        private int channelCount;

        // Token: 0x0400006D RID: 109
        private readonly int DATA_FRAME_LENGTH;
    }
}
